package de.jare.photoswapper;

import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class Preview extends Activity implements SurfaceHolder.Callback {

	private Camera mCamera;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
	private HttpHandler h = new HttpHandler();
	private ProgressDialog pdlg = null;
	private byte[] mData = null;
	private boolean autofocusActive = false;
    
    /* Constructor of this action */
    /* please keep this on top :) */
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

        setContentView(R.layout.preview);

        mSurfaceView = (SurfaceView)findViewById(R.id.surface);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        HttpHandler h = new HttpHandler();
        
    }
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }
 
        if (keyCode == KeyEvent.KEYCODE_CAMERA) {
        	if (autofocusActive == false) {
        		autofocusActive = true;
        		mCamera.autoFocus(afcb);
        	}
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_SPACE) {
        	if (autofocusActive == false) {
        		autofocusActive = true;
        		mCamera.autoFocus(afcb);
        	}
        	return true;
        }
        
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
        	if (autofocusActive == false) {
        		autofocusActive = true;
        		mCamera.autoFocus(afcb);
        	}
        	return true;
    	}
        return false;
    }
	
	public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
    	
        mCamera = Camera.open();
        Camera.Parameters parameters = mCamera.getParameters();
    	parameters.setPictureFormat(PixelFormat.JPEG);
    	parameters.setPictureSize(640, 480);
    	mCamera.setParameters(parameters);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        mCamera.startPreview();
    }
    
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) { 
        	mCamera.stopPreview();
        	
        	h.UploadPicture(data, Preview.this);
        	
        	startGetActivity();
        }
  };
  
  AutoFocusCallback afcb = new AutoFocusCallback() {
	@Override
	public void onAutoFocus(boolean success, Camera camera) {
		if (success)
		{
			mCamera.takePicture(null, null, jpegCallback);
		}
	}
  };
  
  public void startGetActivity() {
	  
	  Intent i = new Intent(this, Get.class);
	  startActivity(i);
	  
	  finish();
  }

}
